package com.linwen.javase.学校;
 
import java.awt.Color;
import java.awt.Container;
//import java.awt.GridLayout;
//import java.awt.datatransfer.Clipboard;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

//import javax.swing.AbstractButton;
import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextField;//文本框
import java.lang.Math;

public class jisuanqi implements ActionListener{
	    private boolean flag;
	    private double d1;
	    private double d2;
	    private String op;
	    
	 JFrame n;
	 JButton jb1;
	 JButton jb2;
	 JButton jb3;
	 JButton jb4;
	 JButton jb5;
	 JButton jb6;
	 JButton jb7;
	 JButton jb8;
	 JButton jb9;
	 JButton jb0;
	 JButton jbsin;
	 JButton jbcos;
	 JButton jbx;
	 JButton jbchu;
	 JButton jbjia;
	 JButton jbjian;
	 JButton jbzhiling;
	 JButton jbqingkong;
	 JButton jbtuige;
	 JButton jbqiuyu;
	 JButton jbdengyu;
	 JButton jbkaifang;
	 JButton jbtan;
     JButton jbdian;
	 JButton jbdaoshu;
	 JButton jbzheng;
	 JButton jbfu;
	 JButton jbpingfang;
	 JButton jbmi;
	 JPanel  jp1;
	 JPanel  jp2;
	 JTextField jf;
	
	 public static void main(String args[]){
		 jisuanqi m=new jisuanqi();
	 }
	 public jisuanqi(){
		 init();
	 }
	 
	public void init(){
		n=new JFrame();
		n.setTitle("计算器");
		n.setBounds(600, 400, 600,400);
		n.setVisible(true);
		n.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		//n.setLayout(null);
		
		jb1=new JButton("1");
		jb2=new JButton("2");
		jb3=new JButton("3");
		jb4=new JButton("4");
		jb5=new JButton("5");
		jb6=new JButton("6");
		jb7=new JButton("7");
		jb8=new JButton("8");
		jb9=new JButton("9");
		jb0=new JButton("0");
		jbsin=new JButton("sin");
		jbcos=new JButton("cos");
		jbx=new JButton("x");
		jbchu=new JButton("/");
		jbjia=new JButton("+");
		jbjian=new JButton("-");
		jbzhiling=new JButton("C");
		jbqingkong=new JButton("CE");
		jbtuige=new JButton("Back");
		jbqiuyu=new JButton("%");
		jbdengyu=new JButton("=");
		jbkaifang=new JButton("sqrt");
		jbtan=new JButton("tan");
	    jbdian=new JButton(".");
		jbdaoshu=new JButton("1/x");
		jbfu=new JButton("±");
		jbpingfang=new JButton("x^2");
		jbmi=new JButton("x^n");
		jp1=new JPanel();
		jp2=new JPanel();
		jf=new JTextField();
	
		 //jp1.setLayout(new GridLayout(6,6));
		 jp1.setLayout(null);
		 jp2.setLayout(null);
		 
		 Container cp=n.getContentPane();
		 
		 cp.add(jp1);
		 cp.add(jp2);
		 jp1.setBounds(0, 50, 600, 400);
		 jp2.setBounds(0, 0, 600, 50);
		 jp1.setBorder(BorderFactory.createLineBorder(Color.black));
		 jp2.setBorder(BorderFactory.createLineBorder(Color.black));
		 
		 jp1.add(jb0);
		 jp1.add(jb1);
		 jp1.add(jb2);
		 jp1.add(jb3);
		 jp1.add(jb4);
		 jp1.add(jb5);
		 jp1.add(jb6);
		 jp1.add(jb7);
		 jp1.add(jb8);
		 jp1.add(jb9);
		 jp1.add(jbsin);		 
		 jp1.add(jbcos);
		 jp1.add(jbx);
		 jp1.add(jbchu);
		 jp1.add(jbjia);
		 jp1.add(jbjian);
		 jp1.add(jbzhiling);
		 jp1.add(jbqingkong);
		 jp1.add(jbtuige);
		 jp1.add(jbqiuyu);
		 jp1.add(jbdengyu);
		 jp1.add(jbkaifang);
		 jp1.add(jbtan);
		 jp1.add(jbdian);
		 jp1.add(jbdaoshu);
		 jp1.add(jbfu);
		 jp1.add(jbpingfang);
		 jp1.add(jbmi);
         jp2.add(jf);
		 
		 jf.setBounds(150, 10, 300,35 );
         
		 jb1.setBounds(100, 10,50,50); 
		 jb2.setBounds(150, 10, 50, 50);
		 jb3.setBounds(200, 10,50,50); 
		 jbjia.setBounds(250, 10,50,50); 
		 jbsin.setBounds(300, 10,100,50); 
		 jbzhiling.setBounds(400, 10,100,50); 
		 jb4.setBounds(100, 60,50,50); 
		 jb5.setBounds(150, 60,50,50); 
		 jb6.setBounds(200, 60,50,50);
		 jbjian.setBounds(250, 60,50,50); 
		 jbcos.setBounds(300, 60,100,50); 
		 jbqingkong.setBounds(400, 60,100,50); 
		 jb7.setBounds(100, 110,50,50); 
		 jb8.setBounds(150, 110,50,50); 
		 jb9.setBounds(200, 110,50,50); 
		 jbx.setBounds(250, 110,50,50); 
		 jbtan.setBounds(300, 110,100,50); 
		 jbtuige.setBounds(400, 110,100,50); 
		 jbdaoshu.setBounds(400, 210,100,50); 
		 jbmi.setBounds(300, 210,100,50); 
		 jb0.setBounds(100, 160,50,50); 
		 jbfu.setBounds(150, 160,50,50); 
		 jbdian.setBounds(200, 160,50,50);
		 jbchu.setBounds(250, 160,50,50); 
		 jbqiuyu.setBounds(300, 160,100,50); 
		 jbdengyu.setBounds(100, 210,100,50); 
		 jbpingfang.setBounds(200, 210,100,50); 
		 jbkaifang.setBounds(400, 160,100,50); 
	
		 
		 jb0.addActionListener(this);
		 jb1.addActionListener(this);
		 jb2.addActionListener(this);
		 jb3.addActionListener(this);
		 jb4.addActionListener(this);
		 jb5.addActionListener(this);
		 jb6.addActionListener(this);
		 jb7.addActionListener(this);
		 jb8.addActionListener(this);
		 jb9.addActionListener(this);
		 jbjia.addActionListener(this);
		 jbsin.addActionListener(this);
		 jbzhiling.addActionListener(this);
		 jbjian.addActionListener(this);
		 jbcos.addActionListener(this);
		 jbqingkong.addActionListener(this);
		 jbx.addActionListener(this);
		 jbtan.addActionListener(this);
		 jbtuige.addActionListener(this);
		 jbdaoshu.addActionListener(this);
		 jbfu.addActionListener(this);
		 jbdian.addActionListener(this);
		 jbchu.addActionListener(this);
		 jbqiuyu.addActionListener(this);
		 jbdengyu.addActionListener(this);
		 jbkaifang.addActionListener(this);
		 jbpingfang.addActionListener(this);
		 jbmi.addActionListener(this);
	}		

		public void actionPerformed(ActionEvent e) {
			// TODO Auto-generated method stub
			if(e.getSource().equals(jb0)){
				if(flag == false&&!jf.getText().equals("0"))
		            jf.setText(jf.getText()+"0");
		        else
		            jf.setText("0");
		        flag = false;
			}
			if(e.getSource().equals(jb1)){
				if(flag == false&&!jf.getText().equals("0"))
		            jf.setText(jf.getText()+"1");
		        else
		            jf.setText("1");
		        flag = false;
			}
			if(e.getSource().equals(jb2)){
				if(flag == false&&!jf.getText().equals("0"))
		            jf.setText(jf.getText()+"2");
		        else
		            jf.setText("2");
		        flag = false;
			}
			if(e.getSource().equals(jb3)){
				if(flag == false&&!jf.getText().equals("0"))
		            jf.setText(jf.getText()+"3");
		        else
		            jf.setText("3");
		        flag = false;
			}
			if(e.getSource().equals(jb4)){
				if(flag == false&&!jf.getText().equals("0"))
		            jf.setText(jf.getText()+"4");
		        else
		            jf.setText("4");
		        flag = false;
			}
			if(e.getSource().equals(jb5)){
				if(flag == false&&!jf.getText().equals("0"))
		            jf.setText(jf.getText()+"5");
		        else
		            jf.setText("5");
		        flag = false;
			}
			if(e.getSource().equals(jb6)){
				if(flag == false&&!jf.getText().equals("0"))
		            jf.setText(jf.getText()+"6");
		        else
		            jf.setText("6");
		        flag = false;
			}
			if(e.getSource().equals(jb7)){
				if(flag == false&&!jf.getText().equals("0"))
		            jf.setText(jf.getText()+"7");
		        else
		            jf.setText("7");
		        flag = false;
			}
			if(e.getSource().equals(jb8)){
				if(flag == false&&!jf.getText().equals("0"))
		            jf.setText(jf.getText()+"8");
		        else
		            jf.setText("8");
		        flag = false;
			}
			if(e.getSource().equals(jb9)){
				if(flag == false&&!jf.getText().equals("0"))
		            jf.setText(jf.getText()+"9");
		        else
		            jf.setText("9");
		        flag = false;
			}
			if(e.getSource().equals(jbsin)){
				double d = Double.parseDouble(jf.getText());
		        jf.setText(""+Math.sin(d));
		        System.out.println(Math.sin(d));
			}
			if(e.getSource().equals(jbcos)){
				 double d = Double.parseDouble(jf.getText());
			        jf.setText(""+Math.cos(d));
			}
			
			if(e.getSource().equals(jbtan)){
				 double d = Double.parseDouble(jf.getText());
			        jf.setText(""+Math.tan(d));
			       
			}
			
			if(e.getSource().equals(jbtuige)){
				  String s = jf.getText();
			        if(!s.equals(""))
			            s = s.substring(0,s.length()-1);
			        if(s.equals(""))
			            s = "0";
			        jf.setText(s);
			}
			
			if(e.getSource().equals(jbqingkong)){
				 jf.setText("");
			        d1 = 0;
			        d2 = 0;
			        op = "";
			        flag = false;
			}
			
			if(e.getSource().equals(jbzhiling)){
			    jf.setText("0");
			}
			
			if(e.getSource().equals(jbjia)){
				//加号
				 d1 = Double.parseDouble(jf.getText());
			        flag = true;
			        op = "+";
			}
			
			if(e.getSource().equals(jbjian)){
				  //减号
		          d1 = Double.parseDouble(jf.getText());
		        flag = true;
		        op = "-";
			}
			
			if(e.getSource().equals(jbx)){
				 //乘号
		        d1 = Double.parseDouble(jf.getText());
		        flag = true;
		        op = "*";
			}
			
			if(e.getSource().equals(jbchu)){
				 //除号
		        d1 = Double.parseDouble(jf.getText());
		        flag = true;
		        op = "/";
			}
			
			if(e.getSource().equals(jbqiuyu)){
				  //求余
		         d1 = Double.parseDouble(jf.getText());
		        flag = true;
		        op = "%";
			}
			
			if(e.getSource().equals(jbkaifang)){
				  //开方
		        double d = Double.parseDouble(jf.getText());
		        jf.setText(""+Math.sqrt(d));
			}
			if(e.getSource().equals(jbpingfang)){
				  //平方
		        double d = Double.parseDouble(jf.getText());
		        jf.setText(""+Math.pow(d,2));
			}
			if(e.getSource().equals(jbmi)){
				  //幂函数
		         d1 = Double.parseDouble(jf.getText());
		        flag = true;
		        op = "x^n";
		       
			}
			if(e.getSource().equals(jbdian)){
				 //点号
		        if(jf.getText().equals("")||flag == true)
		            jf.setText("0.");
		        else
		            jf.setText(jf.getText()+".");
		        flag = false;
			}
			if(e.getSource().equals(jbfu)){
				  //正负号
		        double d = Double.parseDouble(jf.getText());
		        d = 0 - d;
		        jf.setText(""+d);
			}
			if(e.getSource().equals(jbdaoshu)){
				  //求倒数
		        double d = Double.parseDouble(jf.getText());
		        if(d==0)
		        	jf.setText("0");
		        else
		        	jf.setText(""+(1/d));
			}
			if(e.getSource().equals(jbdengyu)){
				    try{op.equals("");}
			        catch (NullPointerException e1){return;}
				 
			        d2 = Double.parseDouble(jf.getText());
			        double result = 0;
			        if(op.equals("+"))
			        {
			            result = d1 + d2;
			        }
			        else if(op.equals("-"))
			        {
			            result = d1 - d2;
			        }
			        else if(op.equals("*"))
			        {
			            result = d1 * d2;
			        }
			        else if(op.equals("/"))
			        {
			            result = d1 / d2;
			            if(d2 == 0)
			                result = 0;
			        }
			        else if(op.equals("%"))
			        {
			            result = d1 % d2;
			            if(d2 == 0)
			                result = 0;
			        }
			        else if(op.equals("x^n")){
			        	result = Math.pow(d1,d2);
			        	if(d2==0)
			        		result=1;
			        }
			        jf.setText(""+result);
			        flag = true;	
			}
		}	
		}